<?php
// +----------------------------------------------------------------------
// | ZanPianCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpian.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.feifeicms.cc>
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;
class Img extends Model{
	//调用接口
	public function down_load($url,$sid='vod',$pyname=''){
		if(is_array($url)){
		$pathArr = array();	
		foreach ($url as $key => $value) {	
		if (config('upload_http') && strpos($value,'://')>0 || config('upload_http_news') && $sid=="news" && strpos($value,'://')>0) {
			$pathArr[]=$this->down_img($value,$sid);
		}else{
			$pathArr[]=$value;
		}
		 }
		 return $pathArr;
			}
		else{
		if (config('upload_http') && strpos($url,'://')>0) {
			return $this->down_img($url,$sid,$pyname);
		}else{
			return $url;
		}
		}
	}
	//远程下载图片
	public function down_img($url,$sid='vod',$pyname=''){
       $chr = strrchr($url,'.');
	   if(!empty($pyname)){
	   $imgUrl = $pyname;    
		   }
	   else{
	   $imgUrl = uniqid();   
		   }
	   $imgPath = $sid.'/'.date(config('upload_style'),time()).'/';	
	   $imgPath_s = './'.config('upload_path').'-s/'.$imgPath;
	   $filename = './'.config('upload_path').'/'.$imgPath.$imgUrl.$chr;
	   $get_file = zanpian_get_url($url,5);
	   if ($get_file) {
		   write_file($filename,$get_file);
		   //是否添加水印
		   if(config('upload_water')){
		   $waterimg=ROOT_PATH.DS.PUBLIC_URL. DS . 'tpl'. DS . 'admin'.DS .config('upload_water_img');
		   @@$image = \think\Image::open($filename);
		   @@$image->water($waterimg,config('upload_water_pos'),config('upload_water_pct'))->save($filename);    
		   }		   
		   //是否生成缩略图
		   if(config('upload_thumb')){
			   mkdirss($imgPath_s);
		       @@$image = \think\Image::open($filename);
			   $upload_thumb=explode('/',trim(config('upload_thumb_size')));
		       @@$image->thumb($upload_thumb[0],$upload_thumb[1],config('upload_thumb_pos'))->save($imgPath_s.$imgUrl.$chr);  
		   }
		   //是否上传远程
		   if (config('upload_ftp')) {
			   $this->ftp_upload($imgPath.$imgUrl.$chr);
		   }
		   return $imgPath.$imgUrl.$chr;
	   }else{
			return $url;
	   } 
	}		
	
    //远程ftp附件	
	public function ftp_upload($imgurl){
		$ftpcon = array(
			'ftp_host'=>config('upload_ftp_host'),
			'ftp_port'=>config('upload_ftp_port'),
			'ftp_user'=>config('upload_ftp_user'),
			'ftp_pwd'=>config('upload_ftp_pass'),
			'ftp_dir'=>config('upload_ftp_dir'),
		);
		$ftp = new \com\Ftp();
		$ftp->config($ftpcon);
		$ftp->connect();
		$ftpimg = $ftp->put(config('upload_path').'/'.$imgurl,config('upload_path').'/'.$imgurl);
		if(config('upload_thumb')){
			$ftpimg_s = $ftp->put(config('upload_path').'-s/'.$imgurl, config('upload_path').'-s/'.$imgurl);
		}
		if(config('upload_ftp_del')){
			if(isset($ftpimg_s)){
				@unlink(config('upload_path').'-s/'.$imgurl);
			}
			if($ftpimg){
				@unlink(config('upload_path').'/'.$imgurl);
			}	
		}
		$ftp->bye();
	}
}